//
//  NIPageView.m
//  NI
//
//  Created by navinfo air on 15/12/30.
//  Copyright © 2015年 oc. All rights reserved.
//

#import "NIPageView.h"

@implementation NIPageView{
    BOOL hasFinishScroll;
    __weak IBOutlet NSLayoutConstraint *pageBottom;
    NSString *keyName;
}

@synthesize delegate;

-(void)awakeFromNib
{
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    
    [self addSubview:self.contentView];
    
    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
    self.scrollView.bounces = NO;
    
    [_pageControl setValue:[UIImage imageNamed:@"Guide_page_v2_normal"] forKeyPath:@"pageImage"];
    [_pageControl setValue:[UIImage imageNamed:@"Guide_page_v2_focus"] forKeyPath:@"currentPageImage"];
    [_contentView setBackgroundColor:[UIColor clearColor]];
}

- (void)setPageImage:(UIImage *)pageImage currentPageImage:(UIImage *)currentPageImage{
    [_pageControl setValue:pageImage forKeyPath:@"pageImage"];
    [_pageControl setValue:currentPageImage forKeyPath:@"currentPageImage"];
}
- (void)setImageArray:(NSArray *)imageArray keyName:(NSString *)name{
    if([keyName isEqualToString:name]){
        return;
    }
    keyName = name;
    for (UIView *view in self.scrollContentView.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            [view removeFromSuperview];
        }
    }
    if ([imageArray  count] > 1) {
        
        self.pageControl.currentPage = 0;
        self.pageControl.numberOfPages = [imageArray count];
        self.pageControl.hidden = NO;
    }else{
        self.pageControl.hidden = YES;
    }
    
    self.scrollContentView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollContentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollContentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    
    [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollContentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
    [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollContentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollContentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollContentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:[imageArray count] constant:0]];
    
    UIImageView *imgView = nil;
    UIImageView *lastImgView = nil;
    for (int i = 0; i<[imageArray count]; i++) {
        imgView = [[UIImageView alloc] initWithImage:[imageArray objectAtIndex:i]];
//        imgView.contentMode = _imageContentMode;
        [imgView setBackgroundColor:[UIColor clearColor]];
        [self.scrollContentView addSubview:imgView];
        imgView.translatesAutoresizingMaskIntoConstraints = NO;
        if (i == 0) {
            [self.scrollContentView addConstraint:[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.scrollContentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
        }else{
            [self.scrollContentView addConstraint:[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:lastImgView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
        }
        [self.scrollContentView addConstraint:[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.scrollContentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
        
        [self.scrollContentView addConstraint:[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.scrollContentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
        
        [self addConstraint:[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
        
        lastImgView = imgView;
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
    pageBottom.constant = 10;
}

- (void)addButton2Page:(UIButton *)button pageIndex:(NSInteger)page{
    [self.scrollContentView addSubview:button];
    
    float screenWidth = [[UIApplication sharedApplication] keyWindow].bounds.size.width;
    float screenHeight = [[UIApplication sharedApplication] keyWindow].bounds.size.height;
    
    float buttonHeight = screenWidth/8;
    float buttonWidth = screenWidth*2/3;
    
    [button setFrame:CGRectMake((screenWidth * page) + (screenWidth - buttonWidth)/2, (screenHeight - buttonHeight*2), buttonWidth, buttonHeight)];
    
    
//    [self addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:(2/3) constant:0]];
//    [self addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:(1/8) constant:0]];
//    [self.scrollContentView addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.scrollContentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:100]];
//    [self.scrollContentView addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.scrollContentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:100]];
    
}

- (void)scrollToPage:(NSInteger)pageIndex{
    [self.scrollView scrollRectToVisible:CGRectMake(0,pageIndex * _contentView.frame.size.width, _contentView.frame.size.width, _contentView.frame.size.height) animated:NO];
}

#pragma mark scrollview delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.pageControl.currentPage = scrollView.contentOffset.x/scrollView.frame.size.width;
    
    if (scrollView.contentOffset.x > (self.pageControl.numberOfPages + 0.3 - 1) * self.scrollView.frame.size.width
        && !hasFinishScroll) {
        hasFinishScroll = YES;
        if (self.delegate && [self.delegate respondsToSelector:@selector(pageDidFinishScroll)]) {
            [self.delegate pageDidFinishScroll];
        }
    }
}

@end
